import Vue from "vue"

//1.1导入路由的包
import VueRouter from "vue-router"
Vue.use(VueRouter)

import VueResource from 'vue-resource'
Vue.use(VueResource)


// //按需导入Mint-UI中的组件
// import { Swipe, SwipeItem } from 'mint-ui';
// Vue.component(Swipe.name, Swipe);
// Vue.component(SwipeItem.name, SwipeItem);
import MintUI from 'mint-ui'
Vue.use(MintUI)
import 'mint-ui/lib/style.css'


import footnav from './common/footnav.vue';
Vue.component('footnav', footnav);

//导入MUI的样式
import '../lib/mui/css/mui.min.css'



// 1.2导入自己的router.js路由模块
import router from './router.js'
//导入字体样式
import fontsize from './js/fontsize.js'

//导入App根组件
import app from './App.vue'
//导入vuex
import Vuex from 'vuex'
Vue.use(Vuex)
    //导入axios
import axios from 'axios';
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
Vue.prototype.$axios = axios;


var store = new Vuex.Store({
    state: {
        car: []
    },
    mutations: {
        addToCar(state, goodsinfo) {
            var flag = false
            state.car.some(item => {
                if (item.skuid == goodsinfo.skuid) {
                    item.count += parseInt(goodsinfo.count)
                    flag = true
                    return true
                }
            })
            if (!flag) {
                state.car.push(goodsinfo)
            }
            localStorage.setItem('car', JSON.stringify(state.car))
        },
        updateGoodsSelected(state, goodsinfo) {
            state.car.some(item => {
                    console.log("panduan" + item.skuid == goodsinfo.skuid)
                    if (item.skuid == goodsinfo.skuid) {
                        item.selected = goodsinfo.selected;
                        return true
                    }
                })
                //把最新的所有购物车商品的状态保存到store中
            localStorage.setItem('car', JSON.stringify(state.car))
        }
    },
    getters: {
        getAllCount(state) {
            var c = 0;
            state.car.forEach(item => {
                c += item.count
            })
            return c
        },
        getGoodsCount(state) {
            var o = {}
            console.log(state.car)
            state.car.forEach(item => {
                o[item.skuid] = item.count

            });
            return o
        },
        getGoodsSelected(state) {
            var o = {}
            state.car.forEach(item => {
                o[item.skuid] = item.selected;
            })
            return o
        },
    }
})
var vm = new Vue({
    el: '#app',
    data: {
        msg: "llaal"
    },
    render: c => c(app),
    router, //1.3挂在路由对象到vm实例上
    fontsize,
    store
});